package com.weichen.studentgameexchange.comment.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.weichen.studentgameexchange.comment.dao.CommentReplyMapper;
import com.weichen.studentgameexchange.comment.entity.CommentReply;
import com.weichen.studentgameexchange.comment.entity.CommentReplyLike;
import com.weichen.studentgameexchange.comment.entity.vo.AddReplyCommentVo;
import com.weichen.studentgameexchange.comment.entity.vo.AddReplyVo;
import com.weichen.studentgameexchange.comment.entity.vo.ReplyVo;
import com.weichen.studentgameexchange.comment.entity.vo.SonReplyVo;
import com.weichen.studentgameexchange.comment.feign.MemberFeignClient;
import com.weichen.studentgameexchange.comment.service.ICommentReplyService;
import com.weichen.studentgameexchange.common.common.Result;
import com.weichen.studentgameexchange.common.enums.ReplyDepth;
import com.weichen.studentgameexchange.member.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 惟尘
 * @since 2025-02-05
 */
@Service
public class CommentReplyServiceImpl extends ServiceImpl<CommentReplyMapper, CommentReply> implements ICommentReplyService {

    @Autowired
    CommentReplyMapper commentReplyMapper;
    @Autowired
    MemberFeignClient memberFeignClient;

    /**
     * 分页查询
     */
    @Override
    public Page<CommentReply> adminPage(Integer page, Integer size) {
        Page<CommentReply> commentReplyPage = new Page<>(page, size);
        LambdaQueryWrapper<CommentReply> commentReplyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        commentReplyLambdaQueryWrapper.orderByDesc(CommentReply::getCommentId);
        return commentReplyMapper.selectPage(commentReplyPage, commentReplyLambdaQueryWrapper);
    }

    /**
     * 删除单个回复
     *
     * @param replyId
     */
    @Override
    public void adminDeleteOne(Long replyId) {
        Db.lambdaUpdate(CommentReplyLike.class)
                .eq(CommentReplyLike::getReplyId, replyId)
                .remove();
        Db.removeById(replyId, CommentReply.class);
    }

    /**
     * 回复评论
     *
     * @param addReplyCommentVo
     * @return
     */
    @Override
    public Result userReplyComment(AddReplyCommentVo addReplyCommentVo) {
        CommentReply reply = BeanUtil.copyProperties(addReplyCommentVo, CommentReply.class);
        reply.setDepth(ReplyDepth.REPLY);
        reply.setPath(reply.getCommentId() + "->");
        commentReplyMapper.insert(reply);
        return Result.success();
    }

    /**
     * 回复回复
     *
     * @param addReplyVo
     * @return
     */
    @Override
    public Result userReply(AddReplyVo addReplyVo) {
        CommentReply reply = BeanUtil.copyProperties(addReplyVo, CommentReply.class);
        reply.setDepth(ReplyDepth.REPLYTO);
        reply.setPath(reply.getPath() + "->");
        commentReplyMapper.insert(reply);
        return Result.success();
    }

    /**
     * 查看评论的回复
     *
     * @param commentId
     * @return
     */
    @Override
    public Result userReplyCommentSelect(Long commentId) {
        List<CommentReply> replies = Db.lambdaQuery(CommentReply.class)
                .eq(CommentReply::getCommentId, commentId)
                .eq(CommentReply::getDepth, ReplyDepth.REPLY)
                .list();
        List<ReplyVo> voList = new ArrayList<>();
        replies.stream().map(reply -> {
            int size = Db.lambdaQuery(CommentReply.class)
                    .eq(CommentReply::getParentReplyId, reply.getReplyId())
                    .list().size();
            ReplyVo replyVo = BeanUtil.copyProperties(reply, ReplyVo.class);
            User user = memberFeignClient.getUserById(replyVo.getUserId());
            replyVo.setUsername(user.getUsername());
            replyVo.setAvatar(user.getAvatar());
            replyVo.setChildCount(size);
            voList.add(replyVo);
            return reply;
        }).toList();
        return Result.success(voList);
    }

    /**
     * 查看回复的回复
     *
     * @param replyId
     * @return
     */
    @Override
    public Result userReplySelect(Long replyId) {
        List<CommentReply> replies = Db.lambdaQuery(CommentReply.class)
                .eq(CommentReply::getParentReplyId, replyId)
                .list();
        List<SonReplyVo> voList = new ArrayList<>();
        replies.stream().map(reply -> {
            SonReplyVo replyVo = BeanUtil.copyProperties(reply, SonReplyVo.class);
            User user = memberFeignClient.getUserById(replyVo.getUserId());
            replyVo.setUsername(user.getUsername());
            replyVo.setAvatar(user.getAvatar());
            voList.add(replyVo);
            return reply;
        }).toList();
        return Result.success(voList);
    }
}
